package com.ruoyi.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.recycle.domain.ClientPrayerManagement;
import com.ruoyi.recycle.domain.RecycleOrder;
import com.ruoyi.recycle.dto.ClientPrayerManagementQuery;
import com.ruoyi.recycle.mapper.ClientPrayerManagementMapper;
import com.ruoyi.recycle.mapper.RecycleOrderMapper;
import com.ruoyi.recycle.service.ClientPrayerManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 许愿管理Service业务层处理
 *
 * @author Wubk
 * @date 2023-06-22
 */
@Service
@Slf4j
public class ClientPrayerManagementServiceImpl extends ServiceImpl<ClientPrayerManagementMapper, ClientPrayerManagement> implements ClientPrayerManagementService {

    @Resource
    private RecycleOrderMapper recycleOrderMapper;


    @Override
    public IPage<ClientPrayerManagement> financePageQuery(ClientPrayerManagementQuery query) throws Exception{

        LambdaQueryWrapper<ClientPrayerManagement> wrapper = getClientPrayerManagementLambdaQueryWrapper(query);

        return this.page(new Page<ClientPrayerManagement>()
                .setCurrent(query.getCurrentPage())
                .setSize(query.getPageSize()), wrapper);
    }

    private LambdaQueryWrapper<ClientPrayerManagement> getClientPrayerManagementLambdaQueryWrapper(ClientPrayerManagementQuery query) throws ParseException {
        ClientPrayerManagement queryEntity = new ClientPrayerManagement();
        BeanUtils.copyProperties(query, queryEntity);
        LambdaQueryWrapper<ClientPrayerManagement> wrapper = Wrappers.lambdaQuery(queryEntity);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("yyyy-MM-dd");
        if (query.getSearchTimeBegin() != null) {
            wrapper.ge(ClientPrayerManagement::getCreatedTime, simpleDateFormat.parse(old.format(query.getSearchTimeBegin()) + " 00:00:00"));
        }
        if (query.getSearchTimeEnd() != null) {
            wrapper.le(ClientPrayerManagement::getCreatedTime, simpleDateFormat.parse(old.format(query.getSearchTimeEnd()) + " 23:59:59"));
        }
        wrapper.orderByDesc(ClientPrayerManagement::getCreatedTime);
        return wrapper;
    }

    @Override
    public List<ClientPrayerManagement> queryClientPrayerManagementList(ClientPrayerManagementQuery query) {
        List<ClientPrayerManagement> result = new ArrayList<>();
        try {
            result = this.list(getClientPrayerManagementLambdaQueryWrapper(query));
        }catch (Exception ex){
            log.error("导出许愿管理失败，失败原因是{}",ex.getMessage(),ex);
        }
        return result;
    }

    @Override
    public void removeClientPrayer(Long[] ids) {
        this.removeByIds(Arrays.asList(ids));
    }

    @Transactional
    @Override
    public AjaxResult saveClientPrayer(ClientPrayerManagement queryDTO) {

        List<RecycleOrder> recycleOrderList = recycleOrderMapper.checkIsAvailableRecycle(queryDTO.getClientId());
        if (CollectionUtils.isEmpty(recycleOrderList)){
            return AjaxResult.error("您暂无回收订单，一个回收订单一个许愿哦~");
        }
        if (containsEmoji(queryDTO.getDetail())){
            return AjaxResult.error("现阶段许愿暂不支持emoji表情哦，请删掉emoji表情后重试~");
        }
        ClientPrayerManagement clientPrayerManagement = new ClientPrayerManagement();
        BeanUtils.copyProperties(queryDTO,clientPrayerManagement);
        clientPrayerManagement.setClientId(queryDTO.getClientId());
        clientPrayerManagement.setRecycleOrderId(recycleOrderList.get(0).getRecycleOrderNo());
        clientPrayerManagement.setCreatedBy(queryDTO.getUserName());
        clientPrayerManagement.setCreatedTime(new Date());
        clientPrayerManagement.setUpdatedBy(queryDTO.getUserName());
        clientPrayerManagement.setUpdatedTime(new Date());
        this.save(clientPrayerManagement);
        return AjaxResult.success("许愿成功，祝你梦想成真！");
    }

    public boolean containsEmoji(String input) {
        String regex = "[\\p{So}\\p{Sk}]";
        return input.matches(".*" + regex + ".*");
    }

}
